package com.example.studentmanager.controller;


import com.example.studentmanager.model.Course;
import com.example.studentmanager.model.StuCourse;
import com.example.studentmanager.model.Student;
import com.example.studentmanager.model.vo.CourseVo;
import com.example.studentmanager.model.vo.GradeVo;
import com.example.studentmanager.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    Student student;

    @Autowired
    StuCourse stuCourse;

    @Autowired
    Course course;



    @RequestMapping("listCourses")
    public String listCourses(HttpSession session, Integer page,String searchCol,String searchValue,Model model){

        //1.获取用户查询的当前页
        page=page==null?1:page;
        //2.获取每页显示的条数
        Integer rows=2;

        //获取用户输入的条件
        System.out.println(searchCol+"----"+searchValue);
        //调用业务层
        //3.求每页显示的成绩列表
        List<CourseVo> courses = courseService.searchAllCourse(page,rows,searchCol,searchValue);

        //4.总记录数
        Integer counts =courseService.counts(searchCol, searchValue);
        //5.总页数
        Integer pages=counts%rows==0?counts/rows:counts/rows+1;
        //2.设置在域中

        model.addAttribute("page", page);
        model.addAttribute("pages", pages);
        model.addAttribute("counts", counts);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("searchValue",searchValue);
        //3.转发到前端页面
        model.addAttribute("courses",courseService.searchAllCourse(page,rows,searchCol,searchValue));

        String pa="";
        if(session!=null){
            if(session.getAttribute("ident").equals("admin")){
                pa="back/course/course";
            }else if(session.getAttribute("ident").equals("student")){
                pa="back/student/select2";
            }
        }
        return pa;
    }


    @RequestMapping("chooseCourse")
    public String  chooseCourse(@RequestParam  String courseid, HttpSession session){
         student=(Student) session.getAttribute("student");
         String status="已选";
        if(student!=null){
            //stuCourse.setSid(student.getStunum());
            //stuCourse.setCid(Integer.valueOf(courseid));
            courseService.addStuCourse(student.getStunum(),Integer.valueOf(courseid).intValue());
            course.setStatus(status);
            course.setCourseid(Integer.valueOf(courseid).intValue());
            courseService.changeCourse(course);


           // courseService.deleteCourse(Integer.valueOf(courseid).intValue());
        }
        return "redirect:/course/listCourses";
    }


    @RequestMapping("cancelCourse")
    public String  cancelCourse(@RequestParam  String courseid, HttpSession session){
        student=(Student) session.getAttribute("student");
        String status="未选";
        if(student!=null){
            //stuCourse.setSid(student.getStunum());
            //stuCourse.setCid(Integer.valueOf(courseid));
           // courseService.addStuCourse(student.getStunum(),Integer.valueOf(courseid).intValue());
            courseService.cancelCourse(student.getStunum(),Integer.valueOf(courseid).intValue());
            course.setStatus(status);
            course.setCourseid(Integer.valueOf(courseid).intValue());
            courseService.changeCourse(course);
            // courseService.deleteCourse(Integer.valueOf(courseid).intValue());
        }
        return "redirect:/course/listCourses";
    }


    @RequestMapping("changeCourse")
    public String changeCourse(Course course){
        System.out.println(course.toString());
        courseService.changeCourse(course);
        return "redirect:/course/listCourses";
    }


    @RequestMapping("getCourse")
    public String getCourse(Course course){
        courseService.addCourse(course);
        return "back/index";
    }

    @RequestMapping("deleteCourse")
    public String deleteCourse(int courseid){
        courseService.deleteCourse(courseid);
        return "back/index";
    }




}
